vue3优雅的使用element 您所在的位置:网站首页 vue3 dom渲染之后 vue3优雅的使用element

vue3优雅的使用element

#vue3优雅的使用element| 来源: 网络整理| 查看: 265

如何优雅的基于 element-plus,封装一个梦中情 dialog 优点

摆脱繁琐的 visible 的命名,以及反复的重复 dom。

想法

将 dialog 封装成一个函数就能唤起的组件。如下:

addDialog({ title: "测试", //弹窗名 component: TestVue, //组件 width: "400px", //弹窗大小 props: { //传给组件的参数 id: 0 }, callBack: (data: any) => { //当弹窗任务结束后,调用父页面的回掉函数。(比如我新增完成了需要刷新列表页面) console.log("回调函数", data) } }) 复制代码 效果图

dialog.gif

基于 el-dialog 进行初步封装 // index.ts import { reactive } from "vue" type dialogOptions = { title: string component: any props?: Object width: string visible?: any callBack?: Function } export const dialogList: dialogOptions[] = reactive([]) export const addDialog = (options: dialogOptions) => { dialogList.push(Object.assign(options, { visible: true })) } export const closeDialog = (item: dialogOptions, i: number, args: any) => { dialogList.splice(i, 1) item.callBack && item.callBack(...args) } 复制代码 import { dialogList, closeDialog } from "./index" 复制代码 首先定义了 dialogList,它包含了所有弹窗的信息。 component 使用 componet is 去动态加载子组件 addDialog 调用唤起弹窗的函数 closeDialog 关闭弹窗的函数 在app.vue中挂载 import Mydialog from "@/components/gDialog/index.vue" 复制代码 使用 创建一个弹窗组件 父弹窗 打开子dialog 关闭弹窗 import { addDialog } from "@/components/gDialog/index" import childVue from "./child.vue" const props = defineProps(["id"]) console.log(props.id, "props") const emit = defineEmits(["close"]) const closeDialog = () => { emit("close", 1, 2, 34) } const openChildDialog = () => { addDialog({ title: "我是子dialog", width: "500px", component: childVue }) } 复制代码 在列表页面唤醒弹窗 列表页 打开dialog import { addDialog } from "@/components/gDialog/index" import TestDialog from "./test.vue" const openDialog = () => { addDialog({ title: "我是dialog", width: "500px", props:{ id:0 } component: TestDialog, callBack: (data: any) => { //当弹窗任务结束后,调用父页面的回掉函数。(比如我新增完成了需要刷新列表页面) console.log("回调函数", data) } }) } 复制代码 多层级弹窗嵌套 子弹窗 关闭弹窗 import { addDialog } from "@/components/gDialog/index" const emit = defineEmits(["close"]) const closeDialog = () => { emit("close", 1, 2, 34) } 复制代码 附上代码

代码

Ending

❀完结撒花❀ 都看到这了 点个赞吧~



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有